package com.vsoontech.audiorecorder;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

class BeepManager {

    // 音频文件bi.pcm是44100采样率，16bit，单声道
    private static final String BI_PCM = "/system/media/audio/notifications/bi.pcm";
    private static final int SAMPLE_RATE = 96000;

    private AudioTrack mAudioTrack;
    private boolean isPlaying = false;
    private boolean isInit = false;

    public BeepManager() {
        int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            bufferSize, AudioTrack.MODE_STREAM);
    }

    public void play() {
        if (isInit) {
            Log.w("BeepManager", "isInit, return");
            return;
        }
        Log.d("BeepManager", "init mAudioTrack");
        //开始播放
        mAudioTrack.play();
        isInit = true;
    }

    public void pushData(byte[] data) {
        mAudioTrack.write(data, 0, data.length);
    }

}